<template>
  <CustomTableCard 
    footer
    :total="total" 
    :page.sync="queryParams.page" 
    :limit.sync="queryParams.limit" 
    :width="1000"
    @on-page="getList">
    <div slot="header">
      <v-row>
        <v-col cols="2">
          <v-select
            v-model="queryParams.type"
            :items="['出团中', '未出团', '已回团']"
            label="选择状态"
            solo
            dense
            outlined
            flat
            hide-details
            clearable
          ></v-select>
        </v-col>
        <v-col cols="2">
          <v-select
            v-model="queryParams.dateType"
            :items="['接团日期', '散团日期', '订单日期', '创建日期']"
            solo
            dense
            outlined
            flat
            hide-details
          ></v-select>
        </v-col>
        <v-col cols="3">
          <el-date-picker
            v-model="queryParams.dateRange"
            type="daterange"
            align="center"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width: 100%">
          </el-date-picker>
        </v-col>
        <v-col cols="2">
          <v-text-field
            label="搜索线路"
            outlined
            dense
            hide-details
          ></v-text-field>
        </v-col>
        <v-col>
          <v-btn color="info" class="mr-3">搜索</v-btn>
        </v-col>
        <v-col cols="2" class="text-right">
          <!-- <v-btn color="primary" @click="showDrawer = true">添加</v-btn> -->
        </v-col>
      </v-row>
    </div>
    <el-table
      v-loading="loading"
      v-auto-height
      :height="tableHeight"
      :data="tableData"
      stripe
      fit
      @selection-change="handleSelectionChange"
      style="width: 100%">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        fixed
        label="状态"
        width="100">
        <template slot-scope="{ row }">
          <v-chip :color="row.plan_date | statusColor(row.back_date)" text-color="white" small>{{row.plan_date | statusText(row.back_date)}}</v-chip>
        </template>
      </el-table-column>
      <el-table-column
        fixed
        show-overflow-tooltip
        prop="code"
        label="团号"
        width="160">
        <template slot-scope="{ row }">
          <v-btn
            color="info"
            x-small
            text
            :to="{path: '/op/detail', query: {id: row.id, cm: 'tourist'}}">
            {{ row.code }}
          </v-btn>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="foundation_prodcut_name"
        label="产品名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="plan_date"
        label="接团日期"
        width="180">
      </el-table-column>
      <el-table-column
        prop="back_date"
        label="散团日期"
        width="180">
      </el-table-column>
      <el-table-column
        prop="day_num"
        label="行程天数"
        width="120">
        <template slot-scope="{ row }">
          <span>{{row.day_num}}天</span>
          <span v-if="row.day_num - 1 > 0">{{row.day_num - 1}}晚</span>
        </template>
      </el-table-column>
      <el-table-column
        label="导游"
        align="center"
        width="80">
        <template slot-scope="{ row }">
          <HoverBook :items="[{name: '水电费第三方', status: 3}]" item-text="name"/>
        </template>
      </el-table-column>
      <el-table-column
        label="酒店"
        align="center"
        width="80">
        <template slot-scope="{ row }">
          <HoverBook :items="[{hotel_name: '大酒店', status: 3}]" item-text="hotel_name"/>
        </template>
      </el-table-column>
      <el-table-column
        label="车辆"
        align="center"
        width="80">
        <template slot-scope="{ row }">
          <HoverBook :items="row.car" item-text="car_name"/>
        </template>
      </el-table-column>
      <el-table-column
        label="餐厅"
        align="center"
        width="80">
        <template slot-scope="{ row }">
          <HoverBook :items="row.hotel" item-text="hotel_name"/>
        </template>
      </el-table-column>
      <el-table-column
        label="景点"
        align="center"
        width="80">
        <template slot-scope="{ row }">
          <HoverBook :items="row.hotel" item-text="hotel_name"/>
        </template>
      </el-table-column>
      <el-table-column
        label="快艇"
        align="center"
        width="80">
        <template slot-scope="{ row }">
          <HoverBook :items="row.boat" item-text="boat_name"/>
        </template>
      </el-table-column>
      <el-table-column
        label="人数"
        width="180">
        <template slot-scope="{ row }">
          <span class="mr-2">{{row.adult_num}}成人</span>
          <span v-if="row.bedkid_num">{{row.bedkid_num}}儿童</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        show-overflow-tooltip
        prop="company_name"
        label="客户名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="operator_a"
        label="联系人"
        width="180">
      </el-table-column>
      <el-table-column
        prop="operator_a_phone"
        label="联系电话"
        width="180">
      </el-table-column>
      <el-table-column
        prop="address"
        label="出发地"
        width="180">
      </el-table-column>
      <el-table-column
        prop="address"
        label="目的地"
        width="180">
      </el-table-column>
      <el-table-column
        prop="address"
        label="所属员工"
        width="180">
      </el-table-column>
      <el-table-column
        prop="address"
        label="所属部门"
        width="180">
      </el-table-column>
      <el-table-column
        prop="address"
        label="订团日期"
        width="180">
      </el-table-column> -->
      <el-table-column
        prop="created_user_name"
        label="创建人"
        width="180">
      </el-table-column>
      <el-table-column
        prop="created_at"
        label="创建时间"
        width="180">
      </el-table-column>
      <el-table-column
        prop="updated_at"
        label="更新时间"
        width="180">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        width="180">
        <template slot-scope="{ row }">
          <v-btn small color="primary" class="mr-2" outlined :to="{path: `/op/detail/${row.id}#base`}">详情</v-btn>
          <v-btn small color="warning" outlined @click="handleRmove(row)">删除</v-btn>
        </template>
      </el-table-column>
    </el-table>
    <div slot="actions">
      <v-btn small outlined color="primary" class="mr-2" :disabled="!multipleSelection.length" @click="handleRmove()">删除</v-btn>
      <v-btn small outlined color="primary" class="mr-2" :disabled="!multipleSelection.length">锁团</v-btn>
      <v-btn small outlined color="primary" class="mr-2" :disabled="!multipleSelection.length">解锁</v-btn>
    </div>
  </CustomTableCard>
</template>

<script>
// @ is an alias to /src
import { mapMutations } from 'vuex'
import mixin from './mixin'
import { getSpPlanList, removeSpOrder } from '@/api/group'
import dayjs from 'dayjs'
import HoverBook from './components/hover-book'

export default {
  name: 'sanpin',
  components: {
    HoverBook
  },
  mixins: [mixin],
  created() {
    this.getList()
  },
  activated() {
    this.getList()
  },
  deactivated() {
    console.log('deactivated')
  },
  mounted() {

  },
  filters: {
    statusColor(plan_date, back_date) {
      let colors = ''
      let currdate = dayjs().format('YYYY-MM-DD')
      if(currdate < plan_date) {
        colors = 'warning'
      } else if(currdate > back_date) {
        colors = 'info'
      } else {
        colors = 'default'
      }
      return colors
    },
    statusText(plan_date, back_date) {
      let text = ''
      let currdate = dayjs().format('YYYY-MM-DD')
      if(currdate < plan_date) {
        text = '未出团'
      } else if(currdate > back_date) {
        text = '已回团'
      } else {
        text = '出团中'
      }
      return text
    }
  },
  data() {
    return {
      loading: false,
      tableData: [{}],
      total: 0,
      queryParams: {
        page: 1,
        limit: 20,
        type: '',
        dateType: '',
        dateRange: [],
      },
      multipleSelection: []
    }
  },

  methods: {
    dayjs,
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    getList(page) {
      if (/\d+/.test(page)) {
        this.queryParams.page = page
      }
      this.loading = true
      getSpPlanList(this.queryParams).then(response => {
        this.tableData = response.data.data
        this.total = response.data.count
      }).finally(() => {
        this.loading = false
      })
    },
    // 删除列表
    handleRmove(row) {
      let ids = []
      if (row) {
        ids = [row.id]
      } else {
        ids = this.multipleSelection.map(item => item.id)
      }
      this.$confirm(this.$t('tips.delete'), this.$t('other.tip'), {
        center: true,
        type: 'warning'
      }).then(() => {
        removeSpOrder({idArr: ids}).then(() => {
          this.$message.success('success')
          this.getList()
        })
      }).catch(() => {})
    },
    // 批量设置状态
    setStatusAll(status) {
      let ids = this.multipleSelection.map(item => item.id)
      this.updateStatus({id: ids, status})
    },
    // 设置状态
    setStatus(row) {
      let status = row.status == 1 ? 2 : 1
      this.updateStatus({id: [row.id], status})
    },
    // 更新状态
    updateStatus(params) {
      systemApi.changeStatus(params).then(() => {
        this.$message.success('success')
        this.getList()
      })
    },
  }
}
</script>
